<!-- 恢复减容 -->
<template>
	<view class="jianRong-dom pb140">
		<uni-section title="基本信息" type="line" style="background: #efeff4;" />
		<uni-card :is-shadow="false" padding="20px 10px 10px" spacing="0" margin="5px 0 5px 0">
			<uni-forms ref="formref" :modelValue="formdata" :rules="formrules" labelWidth="80px" label-position="top"
				class="p20 mb10">
				<uni-forms-item label="用户户号" name="yhbh">
					<uni-easyinput v-model="formdata.yhbh" disabled />
				</uni-forms-item>
				<uni-forms-item label="用户名称" name="yhmc">
					<uni-easyinput v-model="formdata.yhmc" disabled />
				</uni-forms-item>
				<uni-forms-item label="联系电话" name="lxdh">
					<uni-easyinput v-model="formdata.lxdh" disabled />
				</uni-forms-item>
				<uni-forms-item label="用电地址" name="yhdz">
					<uni-easyinput v-model="formdata.yhdz" disabled />
				</uni-forms-item>
			</uni-forms>
		</uni-card>

		<uni-section title="变压器信息" type="line" style="background: #efeff4;">
			<template v-slot:right>
				<view @click="showByqSelect = true">
					<text>选择变压器</text>
					<uni-icons type="right" size="16"></uni-icons>
				</view>
			</template>
		</uni-section>
		<uni-card :is-shadow="false" padding="0" spacing="0" margin="5px 0 0 0" v-for="item in selectByqList">
			<template v-slot:title>
				<uni-list :border="false">
					<uni-list-item :border="false" :title="item.sbmc" />
				</uni-list>
			</template>
			<view class="p40">
				<view class="text-left-right">
					<text>资源编号：{{String(item.zybh)}}</text>
					<text>配电编号：{{String(item.pdbh)}}</text>
				</view>
				<view class="text-left-right">
					<view>设备容量：{{item.sbrlz}}</view>
					<view>
						<text>运行状态：</text>
						<uni-tag v-if="item.dqzt=='0'" text="运行" type="success" />
						<uni-tag v-else text="停止" />
					</view>
				</view>
			</view>
		</uni-card>
	</view>

	<view class="button-submit">
		<button type="primary" @click="formFubmit" style="margin-left: 10px;margin-right:10px;">减容恢复</button>
	</view>

	<cUserinfoSelect v-if="!formdata.yhbh" @setYhbh="doSelectYhbh" />
	<cByqSelect v-if="showByqSelect" :byqList="byqList" @selectByqCancel="showByqSelect = false"
		@selectByq="selectByq" />
</template>

<script setup lang="ts">
	import cByqSelect from "@/pages/bspsd/business/JianRong/components/byq-select.vue";
	import cUserinfoSelect from "@/pages/bspsd/components/userinfo-select.vue";
	import { businessAdd } from "@/api/request/businessAdd";
	import { GetYhJldbyq } from "@/api/request/GetYhJldbyq";
	import { GetYhJbxx } from "@/api/request/GetYhJbxx";
	import { YhJbxx } from "@/api/tables/YhJbxx";
	import { YhJldbyq } from '@/api/tables/YhJldbyq';
	import { SysFiles } from "@/api/tables/SysFiles";

	import { ref, onMounted } from 'vue';

	var dzqm = {} as SysFiles
	let formref = ref(null)
	const showByqSelect = ref(false)
	const byqList = ref<YhJldbyq[]>([])
	const selectByqList = ref<YhJldbyq[]>([])
	const formdata = ref<YhJbxx>({ ywfl: '6', ywlb: '6b', ywzl: '6b1' })
	let formrules = {
		yhbh: { rules: [{ required: true, errorMessage: '请填写用户编号' }] },
		yhmc: { rules: [{ required: true, errorMessage: '请填写用户名称' }] },
		yhdz: { rules: [{ required: true, errorMessage: '请填写用户地址' }] },
	}

	const doSelect = function (check : boolean, item : YhJldbyq) {
		item.select = check
	}

	function formFubmit() {
		let tmp = byqList.value.filter(item => item.select)
		if (tmp.length < 1) {
			uni.showToast({ icon: "error", title: "请选择变压器" })
			return
		}
		formref.value.validate().then(res => {
			if (dzqm.fileId) {
				addClick(dzqm)
			} else {
				uni.navigateTo({
					url: "/pages/bspsd/components/signature",
				})
			}

		}).catch(err => {
			// console.log('表单错误信息：', err);
			uni.showToast({ icon: "none", title: err[0].errorMessage })
		})
	}

	function doSelectYhbh(yhbh : string) {
		GetYhJbxx({
			data: { yhbh: yhbh },
			success: (res) => {
				if (res.length != 1) {
					uni.showModal({ content: "当前用电户号存在异常！", showCancel: false })
					return
				}
				formdata.value = Object.assign({}, res[0], formdata.value)
				doGetYhJldbyq(formdata.value.yhbh)
			},
			fail: (_code, message) => {
				uni.showModal({ content: message, showCancel: false })
			}
		})
	}
	function doGetYhJldbyq(yhbh : string) {
		GetYhJldbyq({
			data: { byq: { yhbh: yhbh, dqzt: "1" } },
			success: (res) => {
				let list = res
				list.forEach(item => {
					item.select = false
				})
				byqList.value = list
			}
		})
	}
	function selectByq(list : YhJldbyq[]) {
		selectByqList.value = list
		showByqSelect.value = false
	}
	onMounted(() => {
		uni.$once("dzqm", (res) => {
			dzqm = res
			addClick(res as SysFiles)
		})
	})

	const addClick = function (dzqm : SysFiles) {
		let list = JSON.parse(JSON.stringify(selectByqList.value))
		if (selectByqList.value.length < 1) {
			uni.showToast({ icon: "error", title: "请选择变压器" })
			return
		}
		const fjs = { dzqm: dzqm }
		let tmp = []
		for (let item of list) {
			item.dqzt = "0"
			tmp.push(item)
		}
		businessAdd({
			data: {
				jbxx: formdata.value,
				fjs: fjs,
				byq: tmp
			},
			success: (result) => {
				uni.showModal({
					title: "提示",
					content: "你的业务已提交成功，稍后可以在：我的->业务办理中查询进度！",
					showCancel: false,
					success: () => {
						uni.navigateBack({ delta: 999 })//跳回首页
					},
				})
			},
			fail: (_code, message) => {
				uni.showModal({
					title: "错误提示", 
					content: "提交失败：" + message,
					showCancel: false,
					success: () => {
						uni.navigateBack()
					}
				})
			}
		})
	}
</script>

<style lang="scss" scoped>
	.jianRong-tab {
		padding: 10px 0;
		text-align: center;
		border: 1px solid #5FB878;

	}

	.jianRong-tab1 {
		padding: 10px 0;
		text-align: center;
		border: 1px solid #5FB878;
		background: #5FB878;
		color: #fff;
	}
</style>